class Admin::ArticlesController < Admin::BaseController  
  def index
    @column_list = %w{id published_at title author}
    @options = {:controller_name => 'admin/articles'}
    #@articles = @this_blog.articles
    @article_pages, @articles, @first_pager, @last_pager, @has_pre_pager, @has_next_pager = sql_paginator(:articles, :per_page => 20, :conditions => "blog_id = #{@this_blog.id}", :order_by => 'published_at desc')
  end
  
  def new
    @article = Article.new
    #@edit_options = {:operation => 'Create New Article', :action_name => 'create', :submit_button => 'Create', :disable_with => 'Creating...'}
  end
  
  def create
    @article = @signed_user.with_options(new_article_defaults) do |usr|
      usr.add_article(params[:article].symbolize_keys)
    end

    if @article.save!
      flash[:notice] = 'Article was successfully created.'
      redirect_to :action => nil
    else
      render :action => 'new'
    end
  end
  
  def new_article_defaults
    return { :user => @signed_user,
      :blog  => @this_blog}
  end
  
  def edit
    @article = Article.find(params[:id])
    @edit_options = {:operation => 'Edit Post', :action_name => 'update', :submit_button => 'Update', :disable_with => 'Updating...'}
  end
  
  def update
    @article = Article.find(params[:id])
    if @article.update_attributes(params[:article])
      flash[:notice] = 'Article was successfully updated.'
      redirect_to :action => nil
    else
      render :action => 'edit'
    end
  end
  
  def destory
    Article.find(params[:id]).destroy
    redirect_to :action => nil
  end
end
